/*
 * @Descripttion: 商品模块中间件
 * @version: 
 * @Author: 一氧化二氢
 * @Date: 2022-01-03 00:01:14
 * @lastEditorTime: Do no edit
 */
/**
 * @name: 
 * @test: test font
 * @msg: 校验文件类型
 * @return {*}
 * @param {*} ctx
 * @param {*} next
 */

const { filrUploadError, unSupportedFileType,goodsFormatError} = require('../constant/err.type')
const checkFileType = async (ctx, next) => {
    const { file } = ctx.request.files
    const fileType = ['image/jpeg', 'image/png']
    try {
        if (file.type && !fileType.includes(file.type)) {
            return ctx.app.emit('error', unSupportedFileType, ctx)
        }
    } catch (err) {
        console.error("文件校验类型出错", err)
        return ctx.app.emit('error', filrUploadError, ctx)
    }
    await next()

}


const validator =async (ctx,next)=>{
        try {
            ctx.verifyParams({
                goods_name:{type:'string',required:true},
                goods_price:{type:'number',required:true},
                goods_num:{type:'number',required:true},
                goods_img:{type:'string',required:true}
            })
        } catch (err) {
            console.error("参数验证出错",err)
            goodsFormatError.result =err
            return ctx.app.emit("error",goodsFormatError,ctx)
        }
        await next()
}

module.exports = {
    checkFileType,
    validator,
}